package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.lne.researchgroupmanage.entity.UserResearch;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.mapper.SysUserMapper;
import xyz.lne.researchgroupmanage.mapper.UserResearchMapper;
import xyz.lne.researchgroupmanage.service.UserResearchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.UserResearchDTO;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@Service
public class UserResearchServiceImpl extends ServiceImpl<UserResearchMapper, UserResearch> implements UserResearchService {

    @Autowired
    private UserResearchMapper userResearchMapper;

    @Override
    public List<UserResearchDTO> findUsersByresearch(Integer researchId) {
        List<UserResearchDTO> userResearchDTOS = new LinkedList<>();

        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchQueryWrapper.eq("status", 1);

        List<UserResearch> userResearches = userResearchMapper.selectList(userResearchQueryWrapper);

        userResearches.forEach(userResearch -> {
            UserResearchDTO userResearchDTO = new UserResearchDTO();
            BeanUtils.copyProperties(userResearch, userResearchDTO);
            userResearchDTOS.add(userResearchDTO);
        });

        return userResearchDTOS;
    }

    @Override
    public List<UserResearchDTO> findApplyUsersByresearch(Integer researchId) {

        List<UserResearchDTO> userResearchDTOS = new LinkedList<>();

        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchQueryWrapper.eq("status", 0);

        List<UserResearch> userResearches = userResearchMapper.selectList(userResearchQueryWrapper);

        userResearches.forEach(userResearch -> {
            UserResearchDTO userResearchDTO = new UserResearchDTO();
            BeanUtils.copyProperties(userResearch, userResearchDTO);
            userResearchDTOS.add(userResearchDTO);
        });

        return userResearchDTOS;
    }

    @Override
    public Boolean deleteUserResearch(Integer researchId) {
        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchMapper.delete(userResearchQueryWrapper);
        return true;
    }

    @Override
    public Integer PassApplyUsersByResearch(Integer researchId, Integer userId) {
        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchQueryWrapper.eq("user_id", userId);

        if (userResearchMapper.selectList(userResearchQueryWrapper).isEmpty()) {
            Map<String, Object> data = new HashMap<>();
            data.put("课题不存在该申请", researchId);
            throw new BadRequestException(data);
        }

        UserResearch userResearch = new UserResearch();
        userResearch.setApproveTime(new Date());
        userResearch.setStatus(1);

        return userResearchMapper.update(userResearch, userResearchQueryWrapper);
    }

    @Override
    public Integer RejectApplyUsersByResearch(Integer researchId, Integer userId) {
        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchQueryWrapper.eq("user_id", userId);

        if (userResearchMapper.selectList(userResearchQueryWrapper).isEmpty()) {
            Map<String, Object> data = new HashMap<>();
            data.put("课题不存在该申请", researchId);
            throw new BadRequestException(data);
        }

        UserResearch userResearch = new UserResearch();
        userResearch.setApproveTime(new Date());
        userResearch.setStatus(2);

        return userResearchMapper.update(userResearch, userResearchQueryWrapper);
    }

    @Override
    public Integer applyToResearch(Integer researchId, Integer userId) {
        QueryWrapper<UserResearch> userResearchQueryWrapper = new QueryWrapper<>();
        userResearchQueryWrapper.eq("research_id", researchId);
        userResearchQueryWrapper.eq("user_id", userId);

        if (!userResearchMapper.selectList(userResearchQueryWrapper).isEmpty()) {
            Map<String, Object> data = new HashMap<>();
            data.put("已经提交了申请", researchId);
            throw new BadRequestException(data);
        }else {
            UserResearch userResearch = new UserResearch();
            userResearch.setStatus(0);
            userResearch.setUserId(userId);
            userResearch.setApplyTime(new Date());
            userResearch.setResearchId(researchId);
            userResearch.setApproveTime(new Date());
            userResearch.setRemark("");
            return userResearchMapper.insert(userResearch);
        }
    }
}
